package com.villaday.idf.activity;

import android.content.Intent;
import android.net.Uri;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.villaday.idf.R;
import com.villaday.idf.activity3.CheckInvoiceActivity;
import com.villaday.idf.activity3.InvoiceApplicationActivity;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.base.BaseDialog;
import com.villaday.idf.custom.ConfirmDialog;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.custom.PayDialog;
import com.villaday.idf.custom.pop.FaPiaoInfoPop;
import com.villaday.idf.model.GatewayOrders;
import com.villaday.idf.model.PayEntity;
import com.villaday.idf.model.PayMent;
import com.villaday.idf.model.gatewayOrdersEntity;
import com.villaday.idf.model.orderEntity;
import com.villaday.idf.model3.FaPiaoconfigurationsEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.DateUtils;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/*
 * 我的订单详情
 * */
public class MyOrderDetailActivity extends BaseActivity implements OkHttpUtil.OnDataListener {


    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;

    @BindView(R.id.sdv_head)
    public SimpleDraweeView sdvHead;
    @BindView(R.id.sdv)
    public SimpleDraweeView sdvImg;
    @BindView(R.id.iv_status)
    public ImageView ivStatus;
    @BindView(R.id.tv_name)
    public TextView tvNickName;

    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.detail)
    public TextView tvDescribe;
    @BindView(R.id.tv_order_num)
    public TextView tvOrderNum;
    @BindView(R.id.tv_constact)
    public TextView tvConstact;
    @BindView(R.id.tv_mobile)
    public TextView tvMobile;
    @BindView(R.id.tv_pay_type)
    public TextView tvPayType;
    @BindView(R.id.tv_pay_balance)
    public TextView tvPayBalance;
    @BindView(R.id.tv_create_order)
    public TextView tvCreateOrder;
    @BindView(R.id.tv_order_status)
    public TextView tvStatus;
    @BindView(R.id.tv_cancel_order)
    public TextView tvCancelOrder;
    @BindView(R.id.tv_into_order)
    public TextView tvDelOrder;

    @BindView(R.id.ll_constact_layout)
    public LinearLayout llServiceLayout;
    @BindView(R.id.tv_origian_price)
    public TextView tvOrigianPrice;
    @BindView(R.id.tv_sale_price)
    public TextView tvSalePrice;
    @BindView(R.id.tv_svip_price)
    public TextView tvSvipPrice;
    @BindView(R.id.tv_user_balance)
    public TextView tvUserBalance;

    @BindView(R.id.ll_house_customer)
    public LinearLayout llHouseCustomer; //房费
    @BindView(R.id.tv_house_customer)
    public TextView tvHouseCustomer;

    @BindView(R.id.ll_optional_offer)
    public LinearLayout llOptionalOffer; //自选优惠
    @BindView(R.id.ll_pay_balance)
    public LinearLayout llUserBalance;//使用余额

    @BindView(R.id.tv_make_appointment)
    public TextView tvBookProperty; //预约入住
    @BindView(R.id.tv_constact_service)
    public TextView tvConstactService;
    @BindView(R.id.tv_again_buy)
    public TextView tvAgainBuy;//再次购买
    @BindView(R.id.tv_payment_order)
    public TextView tvPaymentOrder;//允许购买

    @BindView(R.id.ll_always_times)
    public LinearLayout llAlwaysLayout;//住离日期
    @BindView(R.id.tv_always_time)
    public TextView tvAlwaysTimes;

    @BindView(R.id.ll_hidden_layout)
    public LinearLayout llHiddenLayout;
    @BindView(R.id.tv_see_details)
    public TextView tvSeeDetails;

    @BindView(R.id.ll_single_price)
    public LinearLayout llSinglePrice;//单价
    @BindView(R.id.tv_goods_price)
    public TextView tvSinglePrice;
    @BindView(R.id.tv_goods_num)
    public TextView tvSingleNum;
    @BindView(R.id.tv_line_payment)
    public TextView tvIsLinePayment; //是否在线已支付

    @BindView(R.id.ll_nameAndPhone)
    public LinearLayout ll_nameAndPhone;

    @BindView(R.id.ll_addr)
    public LinearLayout ll_addr;

    @BindView(R.id.tv_nameforaddr)
    public TextView tv_nameforaddr;
    @BindView(R.id.tv_phone)
    public TextView tv_phone;
    @BindView(R.id.tv_addr)
    public TextView tv_addr;

    @BindView(R.id.ll_electric_emails)
    public LinearLayout llElectricEmails;//电子邮箱
    @BindView(R.id.tv_emails)
    public TextView tvEmails;

    @BindView(R.id.ll_id_layout)
    public LinearLayout llID;//身份证ID
    @BindView(R.id.tv_id)
    public TextView tvID;

    @BindView(R.id.ll_connected_offer)
    LinearLayout llConnectOffer;//连住优惠
    @BindView(R.id.tv_connect_offer)
    TextView tvConnectOffer;


    @BindView(R.id.ll_deposit_layout)
    LinearLayout llDeposit;//押金
    @BindView(R.id.tv_deposit)
    TextView tvDeposit;

    @BindView(R.id.v_line0)
    View view;
    @BindView(R.id.ll_remarks)
    LinearLayout llRemarks;//备注布局
    @BindView(R.id.tv_remarks)
    TextView tvMark;//备注信息

    @BindView(R.id.ll_order_fapiaoInfo)
    LinearLayout ll_order_fapiaoInfo;

    @BindView(R.id.tv_order_fapiaoInfo)
    TextView tv_order_fapiaoInfo;
    @BindView(R.id.tv_order_fapiao)
    TextView tv_order_fapiao;

    @BindView(R.id.v_line3)
    View v_line3;

    @BindView(R.id.tv_fapiaoName)
    TextView tv_fapiaoName;
    @BindView(R.id.tv_type)
    TextView tv_type;


    public orderEntity ordersEntity;

    private String OrderNo;
    private LoadingDialog loadingDialog;
    private Gson gson = new GsonBuilder().create();

    @Override
    protected int getViewResId() {
        return R.layout.activity_my_order_detail;
    }

    @Override
    protected void init() {
        ToolBarUtil.initToolBar(toolbarText, "订单详情", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        OrderNo = getIntent().getStringExtra("orderNo");
    }

    @Override
    protected void loadData() {
        //getData(OrderNo);
    }

    private void getData(String orderNo) {
        loadingDialog = LoadingDialog.newInstance("加载中");
        loadingDialog.show(getFragmentManager(), "loading");
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", orderNo);
        OkHttpUtil.getJSON(this, Constant.URL.order, map, this);
    }


    @Override
    public void onResponse(String url, String json) {
        dismissLoading();
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.order.equals(url)) {
                LogeUtil.e("order", json);
                ordersEntity = gson.fromJson(json, orderEntity.class);
                if ("200".equals(ordersEntity.getCode())) {
                    if (ordersEntity.getBody().getInvoice() == null) {
                        if (ordersEntity.getBody().isAllowInvoice()) {
                            ll_order_fapiaoInfo.setVisibility(View.VISIBLE);
                            v_line3.setVisibility(View.VISIBLE);
                        }else {
                            ll_order_fapiaoInfo.setVisibility(View.GONE);
                            v_line3.setVisibility(View.GONE);

                        }

                        tv_order_fapiao.setText("发票申请");
                        tv_fapiaoName.setVisibility(View.GONE);
                        tv_type.setVisibility(View.GONE);

                    } else {
                        ll_order_fapiaoInfo.setVisibility(View.VISIBLE);
                        v_line3.setVisibility(View.VISIBLE);

                        tv_order_fapiao.setText(ordersEntity.getBody().getInvoice().getState().getText());
                        tv_fapiaoName.setText(ordersEntity.getBody().getInvoice().getTitle());
                        tv_type.setText(ordersEntity.getBody().getInvoice().getType());
                        tv_fapiaoName.setVisibility(View.VISIBLE);
                        tv_type.setVisibility(View.VISIBLE);
                    }

                    UpdateUI(ordersEntity.getBody());

                }
            } else if (Constant.URL.gateway_orders.equals(url)) {
                LogeUtil.e("gateway_orders", json);
                gatewayOrdersEntity goe = gson.fromJson(json, gatewayOrdersEntity.class);
                if ("200".equals(goe.getCode())) {
                    getData(goe.getBody().getOrder().getOrderNo());
                }
            }
        }
    }

    @Override
    public void onFailure(String url, String error) {
        dismissLoading();
        ToastUtil.initToast(this, error);
    }

    private void UpdateUI(orderEntity.BodyBean d) {
        switch (d.getStatus()) {
            case 1:
                //ivStatus.setImageResource(R.drawable.icon_warn);
                tvIsLinePayment.setVisibility(View.GONE);
                break;
            case 6:
                //ivStatus.setImageResource(R.drawable.icon_warn);
                tvIsLinePayment.setVisibility(View.GONE);
                break;
            case 5:
                //ivStatus.setImageResource(R.drawable.icon_ok);
                tvIsLinePayment.setVisibility(View.VISIBLE);
                break;
            case 8:
                // ivStatus.setImageResource(R.drawable.icon_ok);
                tvIsLinePayment.setVisibility(View.VISIBLE);
                break;
            case 9:
                //ivStatus.setImageResource(R.drawable.icon_ok);
                tvIsLinePayment.setVisibility(View.VISIBLE);
                break;
            default:
                // ivStatus.setImageResource(R.drawable.icon_dot);
                tvIsLinePayment.setVisibility(View.GONE);
                break;
        }
        Glide.with(this).load(ordersEntity.getBody().getOrderStatus().getIcon()).into(ivStatus);
        tvStatus.setText(d.getStatusText());
        if (d.getAddress() != null) {
            ll_nameAndPhone.setVisibility(View.GONE);
            ll_addr.setVisibility(View.VISIBLE);

            tv_nameforaddr.setText(d.getAddress().getLinkman());
            tv_phone.setText(d.getAddress().getMobile());


            tv_addr.setText(d.getAddress().getAddress());
        }


        sdvHead.setImageURI(PhoneUtil.getHead(SharedPreferencesUtil.getRoleInfo(this).getMember().getHeaderUrl()));
        tvNickName.setText(SharedPreferencesUtil.getRoleInfo(this).getMember().getNickName());
        sdvImg.setImageURI(PhoneUtil.getHead(d.getProduct().getImageUrl()));
        tvTitle.setText(d.getProduct().getName());
        if (TextUtils.isEmpty(d.getProduct().getDesc())) {
            tvDescribe.setVisibility(View.GONE);
            tvDescribe.setVisibility(View.GONE);
        } else {
            tvDescribe.setVisibility(View.VISIBLE);
        }

        tvDescribe.setText(d.getProduct().getDesc());
        tvSinglePrice.setText("¥" + (d.getProduct().getCurrentPrice() / 100));
        tvSingleNum.setText("x" + d.getProduct().getQuantity());
        tvOrderNum.setText(d.getOrderNo());
        tvConstact.setText(d.getLinkman());
        tvMobile.setText(d.getMobile());
        tvCreateOrder.setText(d.getCreateTime());
        tvPayBalance.setText("¥" + d.getActualPriceValue());
        PhoneUtil.setExternalFont(this, tvPayBalance, true);
        tvPayType.setText(d.getPaymentMethod() == 3 ? "支付宝支付" : "微信支付");

        tvOrigianPrice.setText("-0.00");//已改为使用优惠券
//        tvSalePrice.setText(PhoneUtil.handleDecimal2((d.getCurrentPrice() / 100.0) + ""));
        tvSalePrice.setText(d.getCurrentPriceValue());


        if (d.getDiscountAmount() > 0) {
//            tvSvipPrice.setText("-" + PhoneUtil.handleDecimal2((d.getDiscountAmount() / 100.0) + ""));
            tvSvipPrice.setText("-" + d.getDiscountAmountValue());
            llOptionalOffer.setVisibility(View.VISIBLE);
        } else {
            llOptionalOffer.setVisibility(View.GONE);
        }


        if (d.getWalletDeductibleAmount() > 0) {
            tvUserBalance.setText("-" + PhoneUtil.handleDecimal2((d.getWalletDeductibleAmount() / 100.0) + ""));
            llUserBalance.setVisibility(View.VISIBLE);
        } else {
            llUserBalance.setVisibility(View.GONE);
        }


        if (!TextUtils.isEmpty(d.getOriginalPriceValue()) && d.getType() == 0) {
            tvHouseCustomer.setText(d.getOriginalPriceValue()); //房费
            llHouseCustomer.setVisibility(View.VISIBLE);
        } else {
            llHouseCustomer.setVisibility(View.GONE);
        }

        if (TextUtils.isEmpty(d.getDepositAmountValue())) {
            llDeposit.setVisibility(View.GONE);
        } else {
            tvDeposit.setText("+" + d.getDepositAmountValue());
            llDeposit.setVisibility(View.VISIBLE);
        }

        if (TextUtils.isEmpty(d.getStayDiscountAmountValue())) {
            llConnectOffer.setVisibility(View.GONE);
        } else {
            tvConnectOffer.setText("-" + d.getStayDiscountAmountValue());
            llConnectOffer.setVisibility(View.VISIBLE);
        }


        if (TextUtils.isEmpty(d.getIdentityNumber())) {
            llID.setVisibility(View.GONE);
        } else {
            tvID.setText(d.getIdentityNumber());
            llID.setVisibility(View.VISIBLE);
        }

        if (TextUtils.isEmpty(d.getEmail())) {
            llElectricEmails.setVisibility(View.GONE);
        } else {
            tvEmails.setText(d.getEmail());
            llElectricEmails.setVisibility(View.VISIBLE);
        }

        if (TextUtils.isEmpty(d.getIdentityNumber()) && TextUtils.isEmpty(d.getEmail())) {
            view.setVisibility(View.GONE);
        } else {
            view.setVisibility(View.VISIBLE);
        }


        if (!d.isAllowBespeak() && !d.isAllowContact()) {
            llServiceLayout.setVisibility(View.GONE);
        } else {
            llServiceLayout.setVisibility(View.VISIBLE);
        }
        if (d.isAllowCancel()) { //是否允许取消订单
            tvCancelOrder.setVisibility(View.VISIBLE);
        } else {
            tvCancelOrder.setVisibility(View.GONE);
        }
        if (d.isAllowDelete()) { //是否允许删除订单
            tvDelOrder.setVisibility(View.GONE); //已经不允许在订单详情里删除
        } else {
            tvDelOrder.setVisibility(View.GONE);
        }
        if (d.isAllowBespeak()) {  //是否允许预约
            tvBookProperty.setVisibility(View.VISIBLE);
        } else {
            tvBookProperty.setVisibility(View.GONE);
        }
        if (d.isAllowBuy()) {  //是否允许再次购买
            tvAgainBuy.setVisibility(View.VISIBLE);
        } else {
            tvAgainBuy.setVisibility(View.GONE);
        }
        if (d.isAllowPayment()) {  //是否允许支付
            tvPaymentOrder.setVisibility(View.VISIBLE);
        } else {
            tvPaymentOrder.setVisibility(View.GONE);
        }

        if (d.isAllowContact()) { //是否允许联系客服
            tvConstactService.setVisibility(View.VISIBLE);
        } else {
            tvConstactService.setVisibility(View.GONE);
        }

        switch (d.getType()) {
            case 0:   //普通房源
//                tvDescribe.setVisibility(View.VISIBLE);
                llSinglePrice.setVisibility(View.INVISIBLE);
                break;
            case 7://特卖房源
//                tvDescribe.setVisibility(View.GONE);
                llSinglePrice.setVisibility(View.VISIBLE);
                break;
        }

/*
        if (d.getDiscountAmount() <= 0) {
            llOptionalOffer.setVisibility(View.GONE);
        } else {
            llOptionalOffer.setVisibility(View.VISIBLE);
        }

        if (d.getWalletDeductibleAmount() <= 0) {
            llUserBalance.setVisibility(View.GONE);
        } else {
            llUserBalance.setVisibility(View.VISIBLE);
        }
*/

        //住离日期
        if (d.getType() == 0) {
            llAlwaysLayout.setVisibility(View.VISIBLE);
            long days = 0;
            try {
                days = DateUtils.getDatesDays(d.getCheckOutDate(), d.getCheckInDate());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (!TextUtils.isEmpty(d.getCheckInDate()) && !TextUtils.isEmpty(d.getCheckOutDate())) {
                String[] startDate = d.getCheckInDate().split("-");
                String[] endDate = d.getCheckOutDate().split("-");
                String value = startDate[1] + "月" + startDate[2] + "日-" + endDate[1] + "月" + endDate[2] + "日(" + days + "晚)";
                tvAlwaysTimes.setText(value);
            }
        } else {
            llAlwaysLayout.setVisibility(View.GONE);
        }

        if (TextUtils.isEmpty(d.getRemarks())) {
            llRemarks.setVisibility(View.GONE);
        } else {
            llRemarks.setVisibility(View.VISIBLE);
            tvMark.setText(d.getRemarks());
        }
    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }


    @OnClick({R.id.tv_order_fapiaoInfo, R.id.tv_order_fapiao, R.id.tv_copy_btn, R.id.tv_constact_service, R.id.tv_make_appointment, R.id.tv_other_operation
            , R.id.tv_into_order, R.id.tv_cancel_order, R.id.tv_book_property, R.id.tv_again_buy, R.id.tv_payment_order
            , R.id.tv_see_details, R.id.ll_head_layout})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.tv_order_fapiaoInfo://发票弹窗


                loadingDialog = LoadingDialog.newInstance("加载中");
                loadingDialog.show(getFragmentManager(), "loading");
                HashMap<String, String> map = new HashMap<>();
                map.put("key", "invoice_info");
                OkHttpUtil.getJSON(this, Constant.URL.faPiaoconfigurations, map, new OkHttpUtil.OnDataListener() {
                    @Override
                    public void onResponse(String url, String json) {
                        dismissLoading();
                        if (json != null) {
                            LogeUtil.e("faPiaoconfigurations", json);
                            FaPiaoconfigurationsEntity faPiaoconfigurationsEntity = gson.fromJson(json, FaPiaoconfigurationsEntity.class);
                            if (Constant.Strings.SUC.equals(faPiaoconfigurationsEntity.getCode())) {
                                FaPiaoInfoPop faPiaoInfoPop = new FaPiaoInfoPop(MyOrderDetailActivity.this, MyOrderDetailActivity.this, faPiaoconfigurationsEntity.getBody().getContent());
                                faPiaoInfoPop.showPopupWindow();
                            }
                        }

                    }

                    @Override
                    public void onFailure(String url, String error) {
                        dismissLoading();
                    }
                });
                break;
            case R.id.tv_order_fapiao: {//发票
                if (!TextUtils.isEmpty(OrderNo) && ordersEntity != null && ordersEntity.getBody() != null) {
                    if (ordersEntity.getBody().getInvoice() != null) {

                        Intent intent = new Intent(this, CheckInvoiceActivity.class);

                        intent.putExtra("orderNo", getIntent().getStringExtra("orderNo"));
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(this, InvoiceApplicationActivity.class);

                        intent.putExtra("OrderNo", getIntent().getStringExtra("orderNo"));
                        startActivity(intent);
                    }


                }


            }
            break;

            case R.id.ll_head_layout: {

                if (ordersEntity.getBody().getType() == 7) {
                    Intent intent1 = new Intent(this, HouseDetailsActivityFor3.class);
                    intent1.putExtra("houseId", ordersEntity.getBody().getActivity().getId());
                    intent1.putExtra("persellId", ordersEntity.getBody().getActivity().getId());
                    intent1.putExtra("presellProduct", true);
//                    intent.putExtra("type", data.get(position).getType());
                    this.startActivity(intent1);

                } else if (ordersEntity.getBody().getType() == 0) {
                    Intent intent1 = new Intent(this, HouseDetailsActivityFor3.class);
                    intent1.putExtra("houseId", ordersEntity.getBody().getProduct().getId());
                    intent1.putExtra("persellId", ordersEntity.getBody().getProduct().getId());
                    intent1.putExtra("presellProduct", false);
//                    intent.putExtra("type", data.get(position).getType());
                    this.startActivity(intent1);
                }
            }
            break;

            case R.id.tv_copy_btn: {
                PhoneUtil.copyBoard(this, tvOrderNum.getText().toString().trim());
            }
            break;
            case R.id.tv_constact_service: {
//                dialNum(ordersEntity)  0755-26604812
                if (ordersEntity != null) {
                    ConfirmDialog confirmDialog = ConfirmDialog.newInstance("是否拨打电话?\n" + ordersEntity.getBody().getContactMobile(), "取消", "确定");
                    confirmDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
                        @Override
                        public void onItemClick(View v) {
                            switch (v.getId()) {
                                case R.id.btn_confirmDialog:
                                    Intent intent1 = new Intent(Intent.ACTION_DIAL);
                                    Uri data = Uri.parse("tel:" + ordersEntity.getBody().getContactMobile());
                                    intent1.setData(data);
                                    startActivity(intent1);
                                    break;
                            }
                        }
                    });
                    confirmDialog.show(getFragmentManager());
                    break;
                }
            }
            case R.id.tv_make_appointment: {
                if (ordersEntity.getBody().getReservationMethod() == 2 || ordersEntity.getBody().getReservationMethod() == 3) {//2：官方网址预约，3：第三方网址预约
                    Intent intent = new Intent(this, H5Activity.class);
                    intent.putExtra("title", "预约入住");
                    intent.putExtra("url", ordersEntity.getBody().getReservationUrl());
                    startActivity(intent);
                } else if (ordersEntity.getBody().getReservationMethod() == -1) { //-1：无需预约，电话和连接均不支持；2
                } else {
                    dialNum(ordersEntity);
                }
                break;
            }
            case R.id.tv_other_operation: {
                dialNum(ordersEntity);
                break;
            }

            case R.id.tv_into_order: {
                ConfirmDialog confirmDialog = ConfirmDialog.newInstance("确定要删除该订单吗?", "取消", "确定");
                confirmDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
                    @Override
                    public void onItemClick(View v) {

                        switch (v.getId()) {
                            case R.id.btn_confirmDialog: {
                                loadingDialog = LoadingDialog.newInstance("请求中...");
                                loadingDialog.show(getFragmentManager());
                                Map<String, Object> map = new HashMap<>();
                                OkHttpUtil.postJson(MyOrderDetailActivity.this, Constant.URL.gateway_orders, new GatewayOrders(2, ordersEntity.getBody().getOrderNo()), map, new OkHttpUtil.OnDataListener() {
                                    @Override
                                    public void onResponse(String url, String json) {
                                        LogeUtil.e("gateway_orders", json);
                                        gatewayOrdersEntity goe = gson.fromJson(json, gatewayOrdersEntity.class);
                                        if ("200".equals(goe.getCode())) {
                                            finish();
                                        }
                                    }

                                    @Override
                                    public void onFailure(String url, String error) {

                                    }
                                });
                            }
                            break;
                        }

                    }
                });
                confirmDialog.show(getFragmentManager());
                break;
            }


            case R.id.tv_cancel_order: {
                ConfirmDialog confirmDialog = ConfirmDialog.newInstance("确定要取消该订单吗?", "取消", "确定");
                confirmDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
                    @Override
                    public void onItemClick(View v) {
                        switch (v.getId()) {
                            case R.id.btn_confirmDialog: {
                                OperateOrder(1, ordersEntity.getBody().getOrderNo());
                            }
                            break;
                        }
                    }
                });
                confirmDialog.show(getFragmentManager());
            }
            break;

            case R.id.tv_book_property: {//预订房约
                if (ordersEntity.getBody().getReservationMethod() == 2 || ordersEntity.getBody().getReservationMethod() == 3) {//2：官方网址预约，3：第三方网址预约
                    Intent intent = new Intent(this, H5Activity.class);
                    intent.putExtra("title", "预约入住");
                    intent.putExtra("url", ordersEntity.getBody().getReservationUrl());
                    startActivity(intent);
                } else if (ordersEntity.getBody().getReservationMethod() == -1) { //-1：无需预约，电话和连接均不支持；2
                } else {
                    dialNum(ordersEntity);
                }
            }
            break;

            case R.id.tv_again_buy: { //再次购买

                if (ordersEntity.getBody().getType() == 0) { //普通房源
                    Intent intent = new Intent(this, HouseDetailsActivityFor3.class);
                    intent.putExtra("houseId", ordersEntity.getBody().getProduct().getId());
                    intent.putExtra("persellId", "");
//                    intent.putExtra("type", data.get(position).getType());
                    startActivity(intent);

                } else if (ordersEntity.getBody().getType() == 7) { //特卖房源
                    Intent intent = new Intent(this, GoodsAndFodderActivityFor3.class);
                    intent.putExtra("houseId", ordersEntity.getBody().getActivity().getId());
                    intent.putExtra("persellId", ordersEntity.getBody().getActivity().getId());
//                    intent.putExtra("type", data.get(position).getType());
                    startActivity(intent);
                }
                /*if (ordersEntity.getBody().getType() == 7) {
                    Intent intent3 = new Intent(this, CreatOrderActivity.class);
                    intent3.putExtra("type", 7);
                    intent3.putExtra("activity_id", ordersEntity.getBody().getActivity().getId());
                    intent3.putExtra("activity_type", ordersEntity.getBody().getActivity().getType());
                    startActivity(intent3);
                } else if (ordersEntity.getBody().getType() == 0) {
                    Intent intent3 = new Intent(this, ChouseCheckInDateActivity.class);
                    intent3.putExtra("houseId", ordersEntity.getBody().getProduct().getId());
                    intent3.putExtra("type", 0);
                    intent3.putExtra("product_id", ordersEntity.getBody().getProduct().getId());
                    intent3.putExtra("product_type", 100);
                    intent3.putExtra("product_quantity", 1);
                    startActivity(intent3);
                }*/

            }
            break;

            case R.id.tv_payment_order: {
                /*ConfirmDialog confirmDialog = ConfirmDialog.newInstance("确定支付该订单吗?", "取消", "确定");
                confirmDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
                    @Override
                    public void onItemClick(View v) {
                        switch (v.getId()) {
                            case R.id.btn_confirmDialog: {*/
                                if (ordersEntity != null && ordersEntity.getBody() != null) {
                                    if (ordersEntity.getBody().isAllowPayment()) {
                                        final PayDialog payDialog = PayDialog.newInstance(ordersEntity.getBody().getActualPriceValue());
                                        payDialog.show(getFragmentManager());
                                        payDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
                                            @Override
                                            public void onItemClick(View v) {
                                                Map<String, Object> objectObjectHashMap = new HashMap<>();
                                                OkHttpUtil.postJson(MyOrderDetailActivity.this, Constant.URL.pay, new PayMent(ordersEntity.getBody().getOrderNo(), 2), objectObjectHashMap, new OkHttpUtil.OnDataListener() {
                                                    @Override
                                                    public void onResponse(String url, String json) {

                                                        if (!TextUtils.isEmpty(json)) {
                                                            LogeUtil.e("pay11", json);
                                                            SharedPreferencesUtil.saveWeOrderNum(MyOrderDetailActivity.this, ordersEntity.getBody().getOrderNo());
                                                            PayEntity payEntity = gson.fromJson(json, PayEntity.class);
                                                            if ("200".equals(payEntity.getCode())) {
                                                                IWXAPI api = WXAPIFactory.createWXAPI(MyOrderDetailActivity.this, Constant.ID.WeChat_APP_ID, true);
                                                                api.registerApp(Constant.ID.WeChat_APP_ID);
                                                                if (!api.isWXAppInstalled()) {//?????
                                                                    ToastUtil.initToast(MyOrderDetailActivity.this, "??????");
                                                                    return;
                                                                }
                                                                PayEntity.BodyEntity body = payEntity.getBody();
                                                                PayReq req = new PayReq();
                                                                //req.appId = "wxf8b4f85f3a794e77";  // ??????appId
                                                                req.appId = body.getAppid();
                                                                req.partnerId = body.getPartnerid();
                                                                req.prepayId = body.getPrepayid();
                                                                req.nonceStr = body.getNoncestr();
                                                                req.timeStamp = body.getTimestamp();
                                                                req.packageValue = body.getPackageX();
                                                                req.sign = body.getSign();
                                                                req.extData = "app data";
                                                                // ?????????????????????????????????IWXMsg.registerApp???????????
                                                                api.sendReq(req);
                                                                onBackPressed();

                                                            } else {
                                                                ToastUtil.initToast(MyOrderDetailActivity.this, payEntity.getMsg());
                                                            }


                                                        }
                                                    }

                                                    @Override
                                                    public void onFailure(String url, String error) {

                                                    }
                                                });

                                            }
                                        });
                                    }


                                }
                          /*  }
                            break;
                        }


                    }
                });
                confirmDialog.show(getFragmentManager());*/
            }
            break;

            case R.id.tv_see_details: {
                if (tvSeeDetails.isSelected()) {
                    tvSeeDetails.setVisibility(View.VISIBLE);
                    llHiddenLayout.setVisibility(View.GONE);
                } else {
                    tvSeeDetails.setVisibility(View.GONE);
                    llHiddenLayout.setVisibility(View.VISIBLE);
                    tvSeeDetails.setSelected(true);
                }
            }
            break;
        }


    }

    public void OperateOrder(int status, String order) {
        loadingDialog = LoadingDialog.newInstance("请求中...");
        loadingDialog.show(getFragmentManager());
        Map<String, Object> map = new HashMap<>();
        OkHttpUtil.postJson(this, Constant.URL.gateway_orders, new GatewayOrders(status, order), map, this);
    }


    public void dialNum(final orderEntity OEntity) {
        if (OEntity != null) {
            if (OEntity.getBody().isAllowBespeak()) {
                ConfirmDialog confirmDialog = ConfirmDialog.newInstance("是否拨打电话?\n" + OEntity.getBody().getBespeakMobile(), "取消", "确定");
                confirmDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
                    @Override
                    public void onItemClick(View v) {
                        switch (v.getId()) {
                            case R.id.btn_confirmDialog:
                                Intent intent1 = new Intent(Intent.ACTION_DIAL);
                                Uri data = Uri.parse("tel:" + OEntity.getBody().getBespeakMobile());
                                intent1.setData(data);
                                startActivity(intent1);
                                break;
                        }
                    }
                });
                confirmDialog.show(getFragmentManager());
            } else {
                ConfirmDialog confirmDialog = ConfirmDialog.newInstance("是否拨打电话?\n" + OEntity.getBody().getContactMobile(), "取消", "确定");
                confirmDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
                    @Override
                    public void onItemClick(View v) {
                        switch (v.getId()) {
                            case R.id.btn_confirmDialog:
                                Intent intent1 = new Intent(Intent.ACTION_DIAL);
                                Uri data = Uri.parse("tel:" + OEntity.getBody().getContactMobile());
                                intent1.setData(data);
                                startActivity(intent1);
                                break;
                        }
                    }
                });
                confirmDialog.show(getFragmentManager());
            }
        } else {
            ConfirmDialog confirmDialog = ConfirmDialog.newInstance("是否拨打电话?\n" + OEntity.getBody().getContactMobile(), "取消", "确定");
            confirmDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
                @Override
                public void onItemClick(View v) {
                    switch (v.getId()) {
                        case R.id.btn_confirmDialog:
                            Intent intent1 = new Intent(Intent.ACTION_DIAL);
                            Uri data = Uri.parse("tel:" + OEntity.getBody().getContactMobile());
                            intent1.setData(data);
                            startActivity(intent1);
                            break;
                    }
                }
            });
            confirmDialog.show(getFragmentManager());
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        getData(OrderNo);

    }
}
